{	Problem: http://codeforces.ru/problemset/problem/106/A
	Verdict: Accepted
}
uses
    SysUtils, Math;

const
    YES = 'YES';
    NO = 'NO';
    CARD : array[1..9] of char = ('6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A');

var
    ch : char;
    s : string;
    k, t, i : longint;
        
begin 
    read(ch);
    readln;
    readln(S);
    k := 0;
                t := 0;
                if s[2] = ch then k := 50;
                if s[5] = ch then t := 50;
    for i := 1 to 9 do begin
        if CARD[i] = s[1] then 
            inc(k, i);
        if CARD[i] = s[4] then
            inc(t, i);
    end;
    if (k > t)  and ((s[2] = ch) or (s[2] = s[5])) then write(YES) else write(NO);
end.